<?php

namespace app\command;
use app\common\controller\WorkerApi;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\facade\Log;
use Workerman\Lib\Timer;
use Workerman\Worker;

class Chat extends Command
{

    protected function configure()
    {
        // 指令配置
        $this->setName('chat');
        // 设置命令描述
        $this->setDescription('聊天服务');
    }

    protected function execute(Input $input, Output $output)
    {
        global $ws_worker;
        // 指令输出
    	$output->writeln('聊天服务...');
        //websocket协议
        $ws_worker = new Worker("websocket://0.0.0.0:1468");
        //启动4个进程对外提供服务
        $ws_worker->count = 6;
        //设置名称
        $ws_worker->name = 'chat';
        //用户ID组
        $ws_worker->uidConnections = array();
        //uid分组
        $ws_worker->groupConnections = array();
        //启动
        $ws_worker->onWorkerStart = static function($ws_worker)
        {
            $time_interval = 1;
            Timer::add($time_interval, static function () use ($ws_worker) {
                echo "当前时间：".date("Y-m-d H:i:s").PHP_EOL;
            });

            $inner_text_worker = new Worker('text://127.0.0.1:5678');
            //这里的$connection是给推送者调用的
            $inner_text_worker->onMessage = static function ($connection, $buffer) {
                $connection->send( '你好欢迎你');
            };
            $inner_text_worker->listen();
        };

        // 当收到客户端发来的数据后返回hello $data给客户端
        $ws_worker->onMessage = static function($conn, $data)
        {
            if (!empty($data)){
                $info = json_decode($data , true);
                if (!isset($info['type'])){
                    $conn->send('error type');
                }
                switch ($info['type']){
                    case 'login':
                        //验证登录
                        $_SESSION['uid']       = $info['uid'];
                        $_SESSION['conn_id_'.$info['uid']] = $conn->id;
                        WorkerApi::bin_uid($conn,$info['uid']);
                        break;
                    case 'send':
                        if (!empty($info['uid'])){
                            WorkerApi::send_uid($info['uid'],$data);
                        }
                        if (!empty($info['to_uid'])){
                            WorkerApi::send_uid($info['to_uid'],$data);
                        }
                        if (!empty($info['group_id'])){
                            WorkerApi::bin_group($conn,$info['uid'],$info['group_id']);
                            WorkerApi::send_group($info['group_id'],$data);
                        }
                        break;
                }
            }else{
                $conn->send($data);
            }
        };

        // 连接绑定通信ID
        $ws_worker->onConnect = static function($connection)
        {
            $uid = $_SESSION['uid']??$connection->id;
            if ($uid){
                $connection->send($uid.'登录了...');
            }else{
                $connection->send($connection->id.'链接了...');
            }
        };

        // 退出连接
        $ws_worker->onClose = static function ($connection) {
            $uid = $_SESSION['uid']??$connection->id;
            WorkerApi::send_all('zou l');
            WorkerApi::del_uid($uid);
        };
        // 运行worker
        Worker::runAll();
    }
}
